package com.relatev.minecraft.TRAttribute.listener;

import com.relatev.minecraft.TRAttribute.AttributeManager;
import com.relatev.minecraft.TRAttribute.TRAConfigManager;
import com.relatev.minecraft.TRAttribute.TRAttribute;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Projectile;
import org.bukkit.event.Listener;

public class TAListener implements Listener {

    private static TAListener Instance;
    private static AttributeManager AttributeManager;
    private static TRAConfigManager TRACM;
    private Map<Projectile, Double> projectileDamageMap = new HashMap();
    public static boolean DamagePrint = false;

    public static void init() {
        Instance = new TAListener();
        AttributeManager = AttributeManager.Instance;
        TRACM = TRAttribute.mainPlugin.getConfigManager();
        Bukkit.getPluginManager().registerEvents(Instance, TRAttribute.mainPlugin);
    }

//    @EventHandler
//    public void onAttack(EntityDamageByEntityEvent event) {
//        if (event.isCancelled()) {
//            return;
//        }
//        if (event.getEntity() instanceof LivingEntity == false) {
//            return;
//        }
//        LivingEntity target = (LivingEntity) event.getEntity();
//        LivingEntity attacker = getAttacker(event.getDamager());
//        if (attacker == null) {
//            return;
//        }
//        if (DamagePrint) {
//            TRAEntityUtils.sendMessage(target, "DamagerEntityType " + event.getDamager().getType().name());
//            TRAEntityUtils.sendMessage(attacker, "DamagerEntityType " + event.getDamager().getType().name());
//            TRAEntityUtils.sendMessage(attacker, "DamageCause " + event.getCause());
//        }
//        //判断其他尝试
//        double damage;
//        switch (TRACM.getDamageResultMode(event.getCause(), event.getDamager().getType().name())) {
//            case KEEP:
//                damage = event.getDamage();
//                break;
//            case PASS:
//                return;
//            case RESET:
//                damage = 0;
//                break;
//            default:
//                return;
//        }
//        TRAttributeResultEvent revent = new TRAttributeResultEvent(damage, attacker, target, event.getDamager());
//        Bukkit.getPluginManager().callEvent(revent);
//        damage = revent.getDamage();
//        if (DamagePrint) {
//            TRAEntityUtils.sendMessage(target, "damage " + damage);
//            TRAEntityUtils.sendMessage(attacker, "damage " + damage);
//        }
//        if (revent.isCancelled()) {
//            event.setCancelled(true);
//        } else {
//            if (damage < 0) {
//                event.setDamage(0);
//            } else {
//                event.setDamage(damage);
//            }
//        }
//    }

    private LivingEntity getAttacker(Entity damager) {
        if (damager instanceof LivingEntity) {
            return (LivingEntity) damager;
        }
        if (damager instanceof Projectile) {
            Projectile proj = (Projectile) damager;
            if (proj.getShooter() instanceof LivingEntity) {
                return (LivingEntity) proj.getShooter();
            }
        }
        try {
            Object craftdamager = damager.getClass().getMethod("getHandle").invoke(damager);
            Object craftowner = craftdamager.getClass().getDeclaredField("owner").get(craftdamager);
            if (craftowner != null) {
                return (LivingEntity) craftowner.getClass().getMethod("getBukkitEntity").invoke(craftowner);
            }
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | NoSuchFieldException ex) {
            return null;
        }
        return null;
    }
}
